package net.sudot.chess.business.service;

import net.sudot.chess.business.dto.WechatMessageDTO;
import net.sudot.chess.config.WechatBotProperties;
import net.sudot.chess.util.WBihuizhiUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 微信机器人指令
 *
 * @author tangjialin on 2018-11-06.
 */
@Service
public class WechatBotService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private WechatBotProperties wechatBotProperties;
    @Resource
    private RestTemplate restTemplate;

    /**
     * 发送开房指令
     *
     * @param wechatGroupName
     */
    @Async
    public void sendOpenRoomCommand(String wechatGroupName) {
        WechatMessageDTO message = new WechatMessageDTO()
                .setRoom(new WechatMessageDTO.Room().setTopic(wechatGroupName))
                .setContent(WBihuizhiUtils.OPEN_ROOM_COMMAND);
        try {
            restTemplate.postForObject(wechatBotProperties.getDomain() + "/message", message, String.class);
        } catch (Exception e) {
            logger.error("开房指令发送失败", e);
        }
    }
}
